home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Please help ?!
- Date: Sun, 04 Feb 96 12:23:07 GMT
- Organization: none
- Message-ID: <823436587snz@genesis.demon.co.uk>
- References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <TANMOY.96Jan28100725@qcd.lanl.gov> <4eo8m7$647@ns.RezoNet.NET>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4eo8m7$647@ns.RezoNet.NET> ray@ultimate-tech.com "Ray Dunn" writes:
-
- >Unfortunately, many uses of "void *" are far bigger loopholes in type
- >safety than a cast.
-
- Converting to and from void * is in general no more or less safe than casting
- to and from char *. It may be that some compilers warn about a cast from
- char * to a pointer with stricter alignment but the language makes no
- requirement about this. In the particular case of malloc's return value
- you are guaranteed suitable alignment for any object, so it isn't an issue.
-
- > Compilers or lint will pick up most erroneous uses
- >of a cast. A wrong pointer type as the value of a void * function
- >parameter can only be debugged at runtime
-
- How would avoiding void * and using casts help in this particular case?
- If you need to pass pointers to different types through an argument you
- must use a paraneter type that they can all be converted to. Typically that
- can be either void * or char * (requiring a cast). Casting to char *
- doesn't create any alignment issues so it is unlikely that the compiler
- will generate any more useful diagnostics in this case than with using void *.
- In fact you might lose some essential diagnostics (e.g. conversions from
- function pointers).
-
- Anyway the reasons why I don't cast the return value of malloc are:
-
- 1. it results in less cluttered and on balance more readable code. In the
- local area of a malloc call I typically don't care what the actual
- type is, only that the correct amount of memory is allocated for the
- object concerned.
-
- 2. it increases the number of things I can get wrong. An explicit type cast
- is redundant information. All the compiler can do is test the cast
- type against the type of the pointer being assigned to. There is no reason
- why the cast type should be any more accurate than the pointer type.
- If the pointer type was wrong that would very likely show up in the rest
- of the code that tries to use the pointer.
-
- 3. Allowed void * conversions map pretty well onto the target pointer types
- that it is reasonable to use with malloc. Casts are less discerning, even
- if a particular compiler is helpful enough to warn about most problem
- cases. You're putting yourself in the hands of the compiler writer
- especially if you ever have to change versions or development
- platforms.
-
- I'd certainly say that when advocating no casts you should also advocate
- using the form:
-
- ptr = malloc(numelems * sizeof *ptr);
-
- which deals with most issues.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-